﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform.Controls
{
    public class TextBoxBehavior : Behavior<TextBox>
    {
        bool AltGrIsPressed;

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.KeyUp += AssociatedObject_KeyUp;
            this.AssociatedObject.KeyDown += AssociatedObject_KeyDown;
        }

        void AssociatedObject_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Alt)
            {
                AltGrIsPressed = true;
            }
            if (Keyboard.Modifiers == ModifierKeys.Shift || AltGrIsPressed == true)
            {
                e.Handled = true;
            }
            if (e.Handled == false && (e.Key < Key.D0 || e.Key > Key.D9))
            {
                if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
                {
                    if (e.Key != Key.Back)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Alt)
            {
                AltGrIsPressed = false;
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
            this.AssociatedObject.KeyDown -= AssociatedObject_KeyDown;
        }

    }
}
